package com.stu.day11.clone2;

/**
 * Person类重写了clone()方法，在克隆Person对象时，
 * 首先调用super.clone()进行浅克隆，然后对address字段（引用类型）进行深克隆，
 * 即通过调用address.clone()来确保Address对象也被克隆一份。
 * @author zy
 * @date 2025/2/12 12:27
 */
class Person implements Cloneable {
    String name;
    int age;
    Address address;

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 进行浅克隆
        Person cloned = (Person) super.clone();
        // 对引用类型字段进行深克隆
        cloned.address = (Address) address.clone();
        return cloned;
    }
}
